package com.dldata.devtools30.controller.zhibiao;

import com.dldata.devtools30.controller.DevBase30Controller;
import com.dldata.devtools30.entity.DevtZbobjInfoEntity;
import com.dldata.devtools30.repository.zhibiao.DevtZbobjInfoEntityDao;
import com.dldata.devtools30.service.metadata.DevtModelMetadataTypeService;
import com.dldata.devtools30.utils.Result;
import com.dldata.devtools30.utils.StringUtil;
import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping(value = "/DevtZbobjInfoEntityController")
public class DevtZbobjInfoEntityController extends DevBase30Controller {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    
    @Autowired
    DevtZbobjInfoEntityDao zbobjInfoEntityDao;

    @RequestMapping(value = "/getAll")
    @ResponseBody
    public Result getAll(@RequestParam Map<String, String> paramMap){
        String serach=paramMap.get("serach");
        serach=StringUtil.isEmptyOrLength0(serach)?"":serach;
        serach="%"+serach+"%";
        List<DevtZbobjInfoEntity> metadataTypeEntities = zbobjInfoEntityDao.getAll(serach);

        return Result.ok(metadataTypeEntities);
    }
    
    @RequestMapping(value = "/getZbObjDataAll")
    @ResponseBody
    public Result getZbObjDataAll(@RequestParam Map<String, String> paramMap){
        String zbobjid = paramMap.get("zbobjid");
        DevtZbobjInfoEntity zbobjInfoEntity = zbobjInfoEntityDao.findById(zbobjid).orElse(null);
        ArrayList arrayList=new ArrayList();
        if(zbobjInfoEntity!=null){
            String entityZbobjdatasql = zbobjInfoEntity.getZbobjdatasql();
            List<Map<String, Object>> mapList = jdbcTemplate.queryForList(entityZbobjdatasql);
            arrayList.addAll(mapList);
        }
        return Result.ok(arrayList);
    }


    @RequestMapping(value = "/save")
    @ResponseBody
    public Result save(DevtZbobjInfoEntity entity){
        if(StringUtil.isEmptyOrLength0(entity.getZbobjname())){
            return Result.error("指标对象名称不能为空");
        }

        if(StringUtil.isEmptyOrLength0(entity.getId())){
            entity.setId(UUID.randomUUID().toString());
        }
        zbobjInfoEntityDao.save(entity);

        return Result.ok();
    }
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Result delete(@RequestParam Map<String, String> paramMap){

        String id =paramMap.get("id");
        if(StringUtil.isEmptyOrLength0(id)){
            return  Result.error("参数有误");
        }
        try {
            DevtZbobjInfoEntity zbobjInfoEntity = zbobjInfoEntityDao.findById(id).orElse(null);
            if(zbobjInfoEntity!=null){
                zbobjInfoEntityDao.delete(zbobjInfoEntity);
            }
        }
        catch (Exception ex){
            logger.error("【执行"+this.getClass().getName()+".delete() 方法失败 】异常："+ex.getMessage(),ex);
            return  Result.error("操作错误，请稍候再试!");
        }
        return  Result.ok("删除成功");
    }
}
